/*
 * $Id: Changes.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.core.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sourceforge.p4eclipse.core.api.ClientUser;
import net.sourceforge.p4eclipse.core.api.IChangesResult;
import net.sourceforge.p4eclipse.core.api.IPerforceCommand;
import net.sourceforge.p4eclipse.internal.ui.model.ChangesResultAdapterFactory;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.PlatformObject;

/**
  * ClientUser object for the 'p4 changes' command.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Changes extends ClientUser implements IPerforceCommand
{
	private List result = new ArrayList();
	private IStatus status;
	
	/**
	  * Convenience method for creating instances of Changes.
	  */
	public static final Changes newInstance()
	{
		MultiStatus status = new MultiStatus(PerforceUIPlugin.ID, IStatus.OK, "", null); //$NON-NLS-1$
		return new Changes(status);
	}
	
	/**
	  * Creates a new instance.
	  */
	private Changes(MultiStatus status)
	{
		super(status);
		this.status = status;
	}
 
	/** @see IClientUser#outputStat(Map) */
	public void outputStat(Map strDict)
	{
		Result tempResult = new Result();
		tempResult.changelist = Helper.safeGetInteger(strDict, "change"); //$NON-NLS-1$
		tempResult.date = Helper.safeGetDate(strDict, "time"); //$NON-NLS-1$
		tempResult.userName = Helper.safeGetString(strDict, "user"); //$NON-NLS-1$
		tempResult.clientName = Helper.safeGetString(strDict, "client"); //$NON-NLS-1$
		tempResult.status = Helper.safeGetString(strDict, "status"); //$NON-NLS-1$
		tempResult.description = Helper.safeGetString(strDict, "desc"); //$NON-NLS-1$
			
		result.add(tempResult);
	}

	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "changes"; //$NON-NLS-1$
	}

	/**
	  * Returns the result of the operation.
	  */
	public IChangesResult[] getResult()
	{
		return (IChangesResult[])result.toArray(new IChangesResult[result.size()]);
	}

	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return status;
	}

	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}

	/**
	  * Implementation of the IChangesResult. Stores information provided
	  * by the 'p4 changes' command.
	  * 
	  * @see IChangesResult
	  */
	private static class Result extends PlatformObject implements IChangesResult
	{
		private Integer changelist;
		private String clientName;
		private Date date;
		private String description;
		private String status;
		private String userName;
		
		/** @see IChangesResult#getChangelist() */
		public Integer getChangelist() { return changelist; }

		/** @see IChangesResult#getClientName() */
		public String getClientName() { return clientName; }

		/** @see IChangesResult#getDate() */
		public Date getDate() { return date; }

		/** @see IChangesResult#getDescription() */
		public String getDescription() { return description; }

		/** @see IChangesResult#getStatus() */
		public String getStatus() { return status; }

		/** @see IChangesResult#getUserName() */
		public String getUserName() { return userName; }
	}
}
